from flask import request, redirect, url_for, render_template, jsonify
import sys
sys.path.append("..")
from models import UserMsg
from utils import JWT,Serialize

def defineAdminViews(app,db):
    @app.route ('/api/admin/searchAll', methods=['POST'])
    def adminSearchAll():
        user_list = UserMsg.query.all ()
        data = Serialize.serialize_models (user_list)
        return jsonify ({"msg": "获取所有用户信息成功！", "data": data, "code": 200})

    @app.route ('/api/admin/searchDelAll', methods=['POST'])
    def adminSearchDelAll():
        user_list = UserMsg.query.filter_by(is_delete="1").all ()
        data = Serialize.serialize_models (user_list)
        return jsonify ({"msg": "获取所有注销用户信息成功！", "data": data, "code": 200})

    @app.route ('/api/admin/searchUseAll', methods=['POST'])
    def adminSearchUseAll():
        user_list = UserMsg.query.filter_by(is_delete="0").all ()
        data = Serialize.serialize_models (user_list)
        return jsonify ({"msg": "获取所有非注销用户信息成功！", "data": data, "code": 200})

    @app.route ('/api/admin/search')
    def adminSearch():
        username = request.values.get("username")
        # ilike是不区分大小写，like是区分大小写
        user_list = UserMsg.query.filter(UserMsg.username.like(f'%{username}%')).all ()
        data = Serialize.serialize_models (user_list)
        return jsonify ({"msg": "查询用户信息成功！", "data": data, "code": 200})
